package com.zsk.shop.common.utils;

import com.alibaba.fastjson.serializer.ValueFilter;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * 分页工具
 *
 * @author lv
 * @date 2020/05/13 16:07
 */
public class PageWithSqlUtil {

    private static final String INDEX = "index";
    private static final String SIZE = "size";

    //属性值过滤
    private final static ValueFilter valueFilter = new ValueFilter() {
        @Override
        public Object process(Object source, String name, Object value) {
            return value;
        }
    };

    /**
     * 得到分页参数，
     *
     * @param type index-起始值 size-包含长度
     * @return
     */
    public static int get(String type, String limits, String pages) {
        if (ToolUtil.isOneEmpty(limits, pages)) {
            if (type.equals(INDEX)) {
                return 0;
            }
            if (type.equals(SIZE)) {
                return 10;
            }
        }
        int pageInt = Integer.parseInt(pages) - 1;
        int size = Integer.parseInt(limits);
        if (type.equals(INDEX)) {
            //开始
            int index = pageInt * size;
            index = index < 0 ? 0 : index;
            return index;
        } else if (type.equals(SIZE)) {
            return size;
        } else {
            return 0;
        }
    }

    public static int getIndex() {
        //分页数据准备
        HttpServletRequest request = HttpContext.getRequest();
        int index = PageWithSqlUtil.get("index", request.getParameter("limit"), request.getParameter("page"));
        return index;
    }

    public static int getIndex(String page, String limit) {
        page = ToolUtil.isEmpty(page) ? "1" : page;
        limit = ToolUtil.isEmpty(limit) ? "10" : limit;
        int index = PageWithSqlUtil.get("index", limit, page);
        return index;
    }

    public static int getSize() {
        //分页数据准备
        HttpServletRequest request = HttpContext.getRequest();
        int size = PageWithSqlUtil.get("size", request.getParameter("limit"), request.getParameter("page"));
        return size;
    }

    public static int getSize(String page, String limit) {
        page = ToolUtil.isEmpty(page) ? "1" : page;
        limit = ToolUtil.isEmpty(limit) ? "10" : limit;
        int size = PageWithSqlUtil.get("size", limit, page);
        return size;
    }

    public static <T> List<T> getList(List<?> lists, Class<T> clazz) {
        if (lists == null || lists.size() <= 0) {
            return new ArrayList<>();
        }
        int index = PageWithSqlUtil.getIndex();
        int size = PageWithSqlUtil.getSize();
        int endPage = index + size;

        int maxListSize = lists.size();

        if (index > maxListSize) {
            return new ArrayList<>();
        }
        if (endPage > maxListSize) {
            endPage = maxListSize;
        }
        lists = lists.subList(index, endPage);
        List<T> list = new ArrayList<T>();
        for (Object A : lists) {
            list.add(EntityConvertUtils.convertAToB(A, clazz));
        }
        return list;
    }

    public static <T> List<T> getListByJson(int pages, int limit, List<?> lists, int maxListSize, Class<T> clazz) {
        if (lists == null || lists.size() <= 0) {
            return new ArrayList<>();
        }
        int index = PageWithSqlUtil.get("index", limit + "", pages + "");
        int size = PageWithSqlUtil.get("size", limit + "", pages + "");
        int endPage = index + size;

        if (index > maxListSize) {
            return new ArrayList<>();
        }
        if (endPage > maxListSize) {
            endPage = maxListSize;
        }
        lists = lists.subList(index, endPage);
        List<T> list = new ArrayList<T>();
        for (Object A : lists) {
            list.add(EntityConvertUtils.convertAToB(A, clazz));
        }
        return list;
    }
}
